/* eslint-disable @typescript-eslint/ban-ts-comment */
import { defineStore } from 'pinia';
interface objectType {
  [key: string]: string | number | undefined | null | void | object
}

// 设计器内容
export const designerStore = defineStore('designerStore', {
  state: () => ({
    undo: false, // 上一步
    redo: false, // 下一步
    copy: false, // 复制
    cut: false, // 剪切
    paste: false, // 粘贴
    leftPanel: true,
    selectedId: '',
    categoriesParent: <Record<string, any>>{},
    categoriesSubset: <Record<string, any>>{},
    operatorConfig: {},
    workflowInfo: <Record<string, any>>{},
    instanceId: 0,
    instanceState: '',
    homeLoading: false,
    instancesRunData: <Record<string, any>>{},
    canvasLoading: false,
    algoGroup: [],
    workflowSaveState: '',
    workflowSaveTime: 0,
    rightDrawer: false,
    rightDrawerTabType: '',
    tooltipPort: <Record<string, any>>{},
    shortcutKeyActive: false,
    nodePopover: undefined,
    versionId: '',
    experimentId: '',
    events: <any>{
      updateRunState: { index: 0, data: undefined },
      initEvent: { index: 0, data: undefined },
      initInstancesRunInfo: { index: 0, data: undefined },
      clickOrContextmenu: { index: 0, data: undefined },
      setLeftTabCanvas: { index: 0, data: undefined },
      updateWorkflowVersionData: { index: 0, data: undefined },
      historicalVersionHandle: { index: 0, data: undefined},
      resetEvent: { index: 0, data: undefined },
      viewResize: { index: 0, data: undefined },
      loadWorkflowInfo: { index: 0, data: undefined },
      workflowSave: { index: 0, data: undefined },
      setVersionId: { index: 0, data: undefined }
    },
    experimentState: '',
    experimentData: <Record<string, any>>{},
    workflowChange: false,
    flowReadonlyId: '',
    dataReadIdList: <any>[],
    //对应源码app.js部分
    sidebar: {
      opened: true,
      withoutAnimation: false
    },
    device: 'desktop',
    // language: getLanguage(),
    size: 'medium',
    query: {},
    viewResize: 0,
    flowId: '',
    flowInfo: '',
    routeName: '',

    //新增
    operatorData: {},
    template_id: 0,
    workflow_id: 0,
    project_id: '',
    instance_id: 0,
    operator_id: 0,
    allOperator: <any>[],
    saveDag: <Record<string, any>>{},
    selectOptionStr: '',
    workflowWebsocketDag: '',
    LeftTabsState: true,
    experiments_id: <Record<string, any>>{},
  }),
  getters: {
  },
  actions: {
    // 重置参数
    resetDataByReadonly() {
      // this.SET_CATEGORIES_PARENT({})
      this.SET_UNDO(false)
      this.SET_REDO(false)
      this.SET_COPY(false)
      this.SET_CUT(false)
      this.SET_PASTE(false)
      this.SET_SELECTED_ID('')
      this.SET_INSTANCE_ID(0)
      this.SET_INSTANCE_STATE('')
      this.SET_INSTANCES_RUN_DATA({})
      this.SET_WORKFLOW_SAVE_STATE('')
      this.SET_WORKFLOW_SAVE_TIME(0)
      this.SET_RIGHT_DRAWER(false)
      this.SET_RIGHT_DRAWER_TAB_TYPE('')
      this.SET_TOOLTIP_PORT({})
      this.SET_SHORTCUT_KEY_ACTIVE(false)
      this.SET_NODE_POPOVER(undefined)
      this.SET_EXPERIMENT_ID('')
      this.SET_EXPERIMENT_STATE('')
      this.SET_WORKFLOW_CHANGE(false)
    },
    SET_UNDO(undo: boolean) {
      this.workflowChange = undo
    },
    SET_REDO(redo: boolean) {
      this.workflowChange = redo
    },
    SET_COPY(copy: boolean) {
      this.copy = copy
    },
    SET_CUT(cut: boolean) {
      this.cut = cut
    },
    SET_PASTE(paste: boolean) {
      this.paste = paste
    },
    SET_SELECTED_ID(id: string) {
      this.selectedId = id
    },
    SET_INSTANCE_ID(id: number) {
      // 运行实例id
      this.instanceId = id
    },
    SET_INSTANCE_STATE(instanceState: string) {
      // 实例运行状态
      this.instanceState = instanceState
    },
    SET_INSTANCES_RUN_DATA(instancesRunData: object) {
      // 运行数据信息
      this.instancesRunData = instancesRunData
    },
    SET_WORKFLOW_SAVE_STATE(workflowSaveState: string) {
      // 工作流保存请求的状态
      this.workflowSaveState = workflowSaveState
    },
    SET_WORKFLOW_SAVE_TIME(workflowSaveTime: number) {
      // 工作流保存时间
      this.workflowSaveTime = workflowSaveTime
    },
    SET_RIGHT_DRAWER(rightDrawer: boolean) {
      // 右侧面板是否展示
      this.rightDrawer = rightDrawer
    },
    SET_RIGHT_DRAWER_TAB_TYPE(type: string) {
      // 右侧面板展示key
      this.rightDrawerTabType = type
    },
    SET_TOOLTIP_PORT(tooltipPort: object) {
      // 算子链接桩展示数据
      this.tooltipPort = tooltipPort
    },
    SET_SHORTCUT_KEY_ACTIVE(shortcutKeyActive: boolean) {
      // 快捷键面板是否展示
      this.shortcutKeyActive = shortcutKeyActive
    },
    SET_NODE_POPOVER(nodePopover: any) {
      // 节点提示框显示
      this.nodePopover = nodePopover
    },
    SET_VERSION_ID(versionId: any) {
      this.versionId = versionId
    },
    SET_EXPERIMENT_ID(experimentId: any) {
      this.experimentId = experimentId
    },
    // 实验状态
    SET_EXPERIMENT_STATE(experimentState: string) {
      this.experimentState = experimentState
    },
    SET_WORKFLOW_CHANGE(workflowChange: boolean) {
      this.workflowChange = workflowChange
    },

    SET_CATEGORIES_PARENT(obj: object) {
      // 算子父级标题数据
      this.categoriesParent = obj
    },
    SET_CATEGORIES_SUBSET(obj: object) {
      // 算子子集本身数据
      this.categoriesSubset = obj
    },
    SET_OPERATOR_DATA(obj: object) {
      // 算子子集本身数据
      this.operatorData = obj
    },

    SET_OPERATOR_CONFIG(obj: object) {
      // 算子子集配置数据
      this.operatorConfig = obj
    },
    SET_WORKFLOW_INFO(obj: object) {
      // 工作流信息
      this.workflowInfo = obj
    },
    SET_DATA_READ_ID_LIST(data: Array<any>) {
      this.dataReadIdList = data
    },
    SET_LEFT_PANEL(leftPanel: boolean) {
      // 左边栏隐藏
      this.leftPanel = leftPanel
    },

    // 简单的数据响应
    SET_PUBLISH_EVENT(payload: any) {
      if (payload && payload.key) {
        // @ts-ignore
        this.events[payload.key].data = payload.data || undefined
        // @ts-ignore
        this.events[payload.key].index = this.events[payload.key].index + 1
      }
    },
    SET_CANVAS_LOADING(canvasLoading: boolean) {
      // 右侧loading
      this.canvasLoading = canvasLoading
    },

    // CHANGE_SETTING( { key, value }){
    //   // eslint-disable-next-line no-prototype-builtins
    //   if (state.hasOwnProperty(key)) {
    //     state[key] = value
    //     console.log('state')
    //   }
    // }

    // 对应源码app.js部分
    SET_ROUTE_NAME(routeName: string) {
      this.routeName = routeName
    },
    SET_FLOW_ID(flowId: string) {
      this.flowId = flowId
    },
    SET_PROJECT_ID(id: string) {
      this.project_id = id
    },
    SET_WORKFLOW_ID(id: number) {
      this.workflow_id = id
    },
    SET_ALL_OPERATOR(obj: Array<any>) {
      this.allOperator = obj
    },
    // 工作流画布数据
    SAVE_DAG(obj: object) {
      this.saveDag = obj
    },
    SELECT_OPTION_STR(str: string) {
      this.selectOptionStr = str
    },
    WORKFLOW_WEBSOCKET_DAG(str: string) {
      this.workflowWebsocketDag = str
    },
    TEMPLATE_ID(num: number) {
      this.template_id = num
    },
    LEFT_TABS_STATE(bool: boolean) {
      this.LeftTabsState = bool
    },
    EXOERIMENTS_ID(obj: object) {
      this.experiments_id = obj
    }
  },
  persist: {
    enabled: true,
    strategies: [
      {
        storage: sessionStorage,
        paths: ['workflow_id', 'project_id', 'operator_id', 'instance_id', 'template_id', 'LeftTabsState', 'experiments_id','instanceState']
      }
    ]
  }
});
